/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public boolean isPalindrome1(ListNode head) {
        Deque<Integer> stack = new ArrayDeque<>();
        ListNode cur = head;
        while(cur != null){
            stack.push(cur.val);
            cur = cur.next;
        }
        while(head != null){
            if(head.val == stack.pop()){
                 head = head.next;
            }else{
                return false;
            }
        }
        return true;
    }
    // 反转后半部分链表，比较是否回文
    public boolean isPalindrome2(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while(fast.next != null && fast.next.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }// 此时slow在链表中点
        ListNode cur = slow.next;
        slow.next = null;
        ListNode prev = null;
        while(cur != null){
            ListNode temp = cur.next;
            cur.next = prev;
            prev = cur;
            cur = temp;
        }
        while(head != null && prev != null){
            if(head.val == prev.val){
                head = head.next;
                prev = prev.next;
            }else{
                return false;
            }
        }
        return true;

    }
}